package java19_5_14.com.huweilong.dict_service.tcp;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class 长连接之用户输入Client {
    public static void main(String[] args) throws IOException {
        Log.println("准备建立连接");
        Socket socket = new Socket("127.0.0.1", 8080);  // 拨号
        Log.println("连接已经建立");
        Scanner systemInScanner = new Scanner(System.in);

        // 通过 Ctrl + D 结束
        // 通过 Ctrl + Z 结果
        while (systemInScanner.hasNextLine()) {
            String w = systemInScanner.nextLine();

            InputStream is = socket.getInputStream();
            Scanner socketScanner = new Scanner(is, "UTF-8");

            OutputStream os = socket.getOutputStream();
            OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8");
            PrintWriter printWriter = new PrintWriter(writer);

            // 发送请求
            printWriter.printf("我是Java19班的\r\n%s\r\n", w);
            printWriter.flush();

            // 读取响应
            String header = socketScanner.nextLine(); // 好的
            String word = socketScanner.nextLine();   // 苹果
            System.out.println(word);

        }
        //socket.close();         // 挂断电话
    }
}
